package net.xi.dmhome.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.xi.dmhome.entity.*;
import net.xi.dmhome.handler.Result;
import net.xi.dmhome.mapper.AritagMapper;
import net.xi.dmhome.mapper.AritypeMapper;
import net.xi.dmhome.mapper.CommentMapper;
import net.xi.dmhome.mapper.ArticleMapper;
import net.xi.dmhome.service.AritagService;
import net.xi.dmhome.service.AritypeService;
import net.xi.dmhome.service.CommentService;
import net.xi.dmhome.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.swing.text.html.HTML;
import java.util.List;

/**
 * @Author: jxhu
 * @Description: 文章控制层
 * @Date Created in 2021-02-07 16:35
 * @Modified By:
 */
@RestController
@RequestMapping("/article")
public class ArticleController {
    
    @Autowired
    ArticleMapper articleMapper;
    @Autowired
    ArticleService articleService;
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    CommentService commentService;
    @Autowired
    AritypeMapper aritypeMapper;
    @Autowired
    AritagMapper aritagMapper;
    @Autowired
    AritypeService aritypeService;
    @Autowired
    AritagService aritagService;

    /* @Description: 文章信息添加,@Param:  */
    @PostMapping("/addArticle")
    public Result addArticle(@Validated @RequestBody(required = false) Article article) throws Exception {
        int insert = articleMapper.insert(article);
        if (insert != 0) {
            System.out.println("添加成功");
            return Result.success(200, "添加成功", insert);
        } else {
            return Result.fail("添加失败");
        }
    }

    /* @Description: 根据id查找文章,@Param:  */
    @GetMapping("/findarticlebyid/{id}")
    public Result getArticleById(@PathVariable("id") Integer id) throws Exception {
        Article article = articleMapper.selectById(id);

        return Result.success(article);

    }
    /* @Description: 编辑文章,@Param:  */
    @PutMapping("/editArticle")
    public Result editArticle(@Validated @RequestBody Article article) throws Exception {
        boolean update = articleService.saveOrUpdate(article);
        if (update !=false) {

            System.out.println("更改成功");
            return Result.success(200, "success", article);
        } else {
            return Result.fail("更改失败");
        }
    }


    /* @Description: 删除文章,@Param:  */
    @DeleteMapping("/deleteArticle/{id}")
    public Result deleteArticle(@PathVariable("id") Integer id){
        int i = articleMapper.deleteById(id);
        if (i == 1){
            return Result.success("success");
        }else {
            return Result.fail("删除失败");
        }
    }

    /* @Description: 添加评论,@Param:  */
    @PostMapping("/addA")
    public Result addA(@Validated @RequestBody(required = false) Comment comment) throws Exception {

        int insert = commentMapper.insert(comment);
        if (insert != 0) {

            System.out.println("添加成功");
            return Result.success(200, "添加成功", insert);
        } else {
            return Result.fail("添加失败");
        }
    }
    /* @Description: 根据文章id查找对应的评论,@Param:  */
    @GetMapping("/findAbyid/{qid}")
    public Result getCommentById(@PathVariable("qid") Integer qid) throws Exception {
        Comment comment = commentMapper.selectById(qid);


        return Result.success(comment);

    }
    /* @Description: 编辑评论,@Param:  */
    @PutMapping("/editA")
    public Result editComment(@Validated @RequestBody Comment comment) throws Exception {
        boolean update = commentService.saveOrUpdate(comment);
        if (update !=false) {

            System.out.println("更改成功");
            return Result.success(200, "更改成功", comment);
        } else {
            return Result.fail("更改失败");
        }
    }

    /* @Description: 删除评论,@Param:  */
    @DeleteMapping("/deleteA/{id}")
    public Result deleteComment(@PathVariable("id") Integer id){
        int i = commentMapper.deleteById(id);
        if (i == 1){
            return Result.success("success");
        }else {
            return Result.fail("删除失败");
        }
    }

    /* @Description: 文章分类添加,@Param:  */
    @PostMapping("/addtype")
    public Result addAirtype(@Validated @RequestBody(required = false) Aritype aritype){
        int insert = aritypeMapper.insert(aritype);
        if (insert != 0) {

            System.out.println("添加成功");
            return Result.success(200, "success", insert);
        } else {
            return Result.fail("添加失败");
        }
    }

    /* @Description: 文章标签添加,@Param:  */
    @PostMapping("/addtag")
    public Result addAirtag(@Validated @RequestBody(required = false) Aritag aritag){
        int insert = aritagMapper.insert(aritag);
        if (insert != 0) {

            System.out.println("添加成功");
            return Result.success(200, "success", insert);
        } else {
            return Result.fail("添加失败");
        }
    }

    /* @Description: 文章分类编辑@Param:  */
    @PutMapping("/edittype")
    public Result editAirtype(@Validated @RequestBody(required = false) Aritype aritype){
        int insert = aritypeMapper.updateById(aritype);
        if (insert != 0) {

            System.out.println("修改成功");
            return Result.success(200, "success", insert);
        } else {
            return Result.fail("添加失败");
        }
    }

    /* @Description: 文章标签编辑@Param:  */
    @PutMapping("/edittag")
    public Result editAirtag(@Validated @RequestBody(required = false) Aritag aritag){
        int insert = aritagMapper.updateById(aritag);
        if (insert != 0) {

            System.out.println("修改成功");
            return Result.success(200, "success", insert);
        } else {
            return Result.fail("添加失败");
        }
    }

    /* @Description: 删除文章类型,@Param:  */
    @DeleteMapping("/deleteAritype/{id}")
    public Result deleteAritype(@PathVariable("id") Integer id){
        int i = aritypeMapper.deleteById(id);
        if (i == 1){
            return Result.success("success");
        }else {
            return Result.fail("删除失败");
        }
    }

    /* @Description: 删除文章标签,@Param:  */
    @DeleteMapping("/deleteAritag/{id}")
    public Result deleteAritag(@PathVariable("id") Integer id){
        int i = aritagMapper.deleteById(id);
        if (i == 1){
            return Result.success("success");
        }else {
            return Result.fail("删除失败");
        }
    }

    /* @Description: 展示分类,@Param:  */
    @GetMapping("/listType")
    public Result listtype(){
        List<Aritype> list = aritypeService.list();
        return Result.success(list);
    }
    /* @Description: 展示标签,@Param:  */
    @GetMapping("/listTag")
    public Result listtag(){
        List<Aritag> list = aritagService.list();
        return Result.success(list);
    }

    /* @Description: 根据标签查询文章,@Param:  */
    @GetMapping("/listArticlebyTag/{id}")
    public Result listArticleTag(@PathVariable("id") Integer id,@RequestParam(defaultValue = "1") Integer currentPage){
        Aritag tag = aritagMapper.selectById(id);
        Page page = new Page(currentPage,5);
        IPage iPage = articleService.page(page, new QueryWrapper<Article>()
                .like("tag",tag.getAritagcontent()).orderByDesc("createtime"));

        return Result.success(iPage);

    }
    /* @Description: 根据分类查询文章,@Param:  */
    @GetMapping("/listArticlebyType/{id}")
    public Result listArticleType(@PathVariable("id") Integer id,@RequestParam(defaultValue = "1") Integer currentPage){
        Aritype aritype = aritypeMapper.selectById(id);
        Page page = new Page(currentPage,5);
        IPage iPage = articleService.page(page, new QueryWrapper<Article>()
                .like("type",aritype.getAritypecontent()).orderByDesc("createtime"));

        return Result.success(iPage);

    }



}
